1 using UnityEngine;
2 using System.Collections;
3
4 [RequireComponent (typeof (Detonator))]
5 [AddComponentMenu("Detonator/Heatwave (Pro Only)")]
6 public class DetonatorHeatwave : DetonatorComponent {
7
8 private GameObject _heatwave;
9
10 private float s;
11 private float _startSize;
12 private float _maxSize;
13 private float _baseDuration = .25f;
14 private bool _delayedExplosionStarted = false;
15 private float _explodeDelay;
16
17 public float zOffset = .5f;
18 public float distortion = 64;
19
20 private float _elapsedTime = 0f;
21 private float _normalizedTime;
22
23 public Material heatwaveMaterial;
24 private Material _material; //tmp material we alter at runtime;
25
26 override public void Init()
27 {
28 //we don't want to do anything until we explode
29 }
30
31 void Update ()
32 {
33 if (_delayedExplosionStarted)
34 {
35 _explodeDelay = (_explodeDelay - Time.deltaTime);
36 if (_explodeDelay <= 0f)
37 {
38 Explode();
39 }
40 }
41
42 //_heatwave doesn't get defined unless SystemInfo.supportsImageEffects is true, checked in Explode()
43 if (_heatwave)
44 {
45 // billboard it so it always faces the camera - can't use regular lookat because the built in Unity plane is lame
46 _heatwave.transform.rotation = Quaternion.FromToRotation(Vector3.up, Camera.main.transform.position - _heatwave.transform.position);
47 _heatwave.transform.localPosition = localPosition + (Vector3.forward * zOffset);
48
49 _elapsedTime = _elapsedTime + Time.deltaTime;
50 _normalizedTime = _elapsedTime/duration;
51
52 //thought about this, and really, the wave would move linearly, fading in amplitude.
53 s = Mathf.Lerp(_startSize,_maxSize,_normalizedTime);
54
55 _heatwave.GetComponent<Renderer>().material.SetFloat("_BumpAmt", ((1-_normalizedTime) * distortion));
56
57 _heatwave.gameObject.transform.localScale = new Vector3(s,s,s);
58 if (_elapsedTime > duration) Destroy(_heatwave.gameObject);
59 }
60 }
61
62 override public void Explode()
63 {
64 //try to early out if we can't draw this (not sure if this also gets us out of Unity Indie)
65 if (SystemInfo.supportsImageEffects)
66 {
67 if ((detailThreshold > detail) || !on) return;
68
69 if (!_delayedExplosionStarted)
70 {
71 _explodeDelay = explodeDelayMin + (Random.value * (explodeDelayMax - explodeDelayMin));
72 }
73 if (_explodeDelay <= 0)
74 {
75 //incoming size is based on 1, so we multiply here
76 _startSize = 0f;
77 _maxSize = size * 10f;
78
79 _material = new Material(Shader.Find("HeatDistort"));
80 _heatwave = GameObject.CreatePrimitive(PrimitiveType.Plane);
81 _heatwave.name = "Heatwave";
82 _heatwave.transform.parent = this.transform;
83 Destroy(_heatwave.GetComponent(typeof(MeshCollider)));
84
85 if (!heatwaveMaterial) heatwaveMaterial = MyDetonator().heatwaveMaterial;
86 _material.CopyPropertiesFromMaterial(heatwaveMaterial);
87 _heatwave.GetComponent<Renderer>().material = _material;
88 _heatwave.transform.parent = this.transform;
89
90 _delayedExplosionStarted = false;
91 _explodeDelay = 0f;
92 }
93 else
94 {
95 _delayedExplosionStarted = true;
96 }
97 }
98 }
99
100 public void Reset()
101 {
102 duration = _baseDuration;
103 }
104 }